﻿using System;
using System.Drawing;
using System.IO;
using System.Net;

namespace Wheels
{
    public class MapCreator
    {
        private const double X_METERS = 110000;
        private const double Y_METERS = 145000;

        private static readonly double[] metersInZoom = new[]
                                                            {
                                                                78271.52,
                                                                39135.76,
                                                                19567.88,
                                                                9783.94,
                                                                4891.97,
                                                                2445.98,
                                                                1222.99,
                                                                611.50,
                                                                305.75,
                                                                152.87,
                                                                76.44,
                                                                38.22,
                                                                19.11,
                                                                9.55,
                                                                4.78,
                                                                2.39,
                                                                1.19,
                                                                0.60,
                                                                0.30
                                                            };

        private static Stream DownloadData(string url)
        {
            WebRequest req = WebRequest.Create(url);
            WebResponse response = req.GetResponse();
            Stream stream = response.GetResponseStream();
            return stream;
        }

        private static Point CoordinateToViewPoint(double lat, double lon, double centerlat, double centerlon, int zoom,
                                                   double imgWidth, double imgHeight)
        {
            double mInPix = metersInZoom[zoom - 1];
            double lat1 = centerlat - ((imgHeight/2)*mInPix)/Y_METERS;
            double lat2 = centerlat + ((imgHeight/2)*mInPix)/Y_METERS;
            double lon1 = centerlon - ((imgWidth/2)*mInPix)/X_METERS;
            double lon2 = centerlon + ((imgWidth/2)*mInPix)/X_METERS;

            double x = imgWidth*(lon - lon1)/(lon2 - lon1);
            double y = imgHeight - imgHeight*(lat - lat1)/(lat2 - lat1);
            return new Point((int) x, (int) y);
        }

        // AB: Needs to be public in order to compile the project
        public static double Radius(int zoom, double imgWidth, double imgHeight)
        {
            double mInPix = metersInZoom[zoom - 1];
            double radLat = ((imgHeight/2)*mInPix)/Y_METERS;
            double radLon = ((imgWidth/2)*mInPix)/X_METERS;
            return Math.Max(radLat, radLon);
        }

        public static int calculateZoom(double northeastLat, double northeastLong,
                                        double southwestLat, double southwestLong, double imgWidth, double imgHeight)
        {
            double centerlat = (northeastLat + southwestLat)/2;
            double centerlon = (northeastLong + southwestLong)/2;

            int zoom = 1;
            for (int i = 0; i < metersInZoom.Length; i++)
            {
                zoom = i + 1;
                Point tmp = CoordinateToViewPoint(northeastLat, northeastLong, centerlat, centerlon, zoom, imgWidth,
                                                  imgHeight);
                if (tmp.X > imgWidth || tmp.Y > imgHeight)
                {
                    zoom--;
                    break;
                }
            }
            return zoom;
        }
    }
}